# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html


# class StackPipeline(object):
#     def process_item(self, item, spider):
#         return item

import redis
import json
import uuid

# from redisearch import Client, TextField, NumericField, Query

from scrapy.conf import settings
from scrapy.exceptions import DropItem
from scrapy import log

class RedisPipeline(object):

    def __init__(self):
        self.redis_client = redis.Redis(**{
            'host': settings['REDIS_SERVER'],##redis 保护模式破除 config set protected-mode "no"
            'port': settings['REDIS_SERVER_PORT'],
            'db':0
        })
        # self.redisearch_client = Client(
        #     settings['REDIS_INDEX_NAME'], 
        #     settings['REDIS_SERVER'],
        #     settings['REDIS_SERVER_PORT']
        # )


    def process_item(self, item, spider):
        for data in item:
            if not data:
                raise DropItem("Missing data!")
        print '------------------------------------------------------------------------------------------'
        print data
        print '------------------------------------------------------------------------------------------'
        self.redis_client.hset('m:blog:urls', item['url'], item['body'])
        self.redis_client.sadd('set:blog:urls', item['url'])
        
        # self.redisearch_client.create_index([TextField('title'), TextField('url'), TextField('labels')])
        # FT.CREATE s:blog SCHEMA title TEXT url TEXT labels TEXT
        self.redisearch_client.add_document(
            str(uuid.uuid1()),
            title = item['title'],
            url = 'https://stackoverflow.com' + item['url'],
            labels = item['tags']
        )